<%@page import="cn.cecook.utils.CookieUtil "%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>积分列表</title>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/bootcdn/BootCDN/ajax/libs/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/css/font-awesome.min93e3.css"/>
<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/css/plugins/iCheck/custom.css"/>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.15/css/dataTables.bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/css/style.min.css"/>
<style type="text/css">
body{font-size: 14px;padding: 10px;}
#condition div.row{margin-bottom: 20px;}
.dataTables_info {font-size: 12px}
.dataTables_paginate {font-size: 12px;}
#myTable td:last-child a{color: #1ab394;font-weight: bold;font-size: 12px;}
</style>
</head>
<body class="gray-bg">
<div class="container-fluid">
	<div id="condition" style="background-color: white;padding: 20px;line-height: 34px;">
		<form action="">
			<div class="row">
				<div class="col-sm-1 text-right">
					积分状态
				</div>
				<div class="col-sm-2">
					<select id="integral_type" class="form-control">
						<option value="">不限</option>
						<option value="1">正常</option>
						<option value="2">冻结</option>
					</select>
				</div>
				<div class="col-sm-1 text-right">
					手机号
				</div>
				<div class="col-sm-2">
					<input id="phone" class="form-control">
				</div>
				<div class="col-sm-1 text-right">
					会员等级
				</div>
				<div class="col-sm-2">
					<input id="crm_member_level" class="form-control">
					<!-- 
					<select class="form-control">
						<option>不限</option>
					</select>
					 -->
				</div>
				<div class="col-sm-1 text-right">
					消费金额
				</div>
				<div class="col-sm-2">
					<input id="cost_total1" class="form-control" style="display: inline-block;width: 60px;">至
					<input id="cost_total2" class="form-control" style="display: inline-block;width: 60px;">
				</div>
			</div>
			<div class="row">
				<div class="col-sm-1 text-right">
					会员状态
				</div>
				<div class="col-sm-2">
					<select id="type" class="form-control">
						<option value="">不限</option>
						<option value="1">正常</option>
						<option value="2">冻结</option>
					</select>
				</div>
				<div class="col-sm-1 text-right">
					积分余额
				</div>
				<div class="col-sm-2">
					<input id="point1" class="form-control" style="display: inline-block;width: 60px;">至
					<input id="point2" class="form-control" style="display: inline-block;width: 60px;">
				</div>
				<div class="col-sm-1 text-right">
					使用积分
				</div>
				<div class="col-sm-2">
					<input id="use_integral1" class="form-control" style="display: inline-block;width: 60px;">至
					<input id="use_integral2" class="form-control" style="display: inline-block;width: 60px;">
				</div>
			</div>
			<div class="row text-center">
				<button onclick="func_search()" class="btn btn-primary" type="button">查询</button>
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		        <button onclick="func_reset()" class="btn btn-white" type="button" style="border-color: #18a689;">重置</button>
			</div>
		</form>
	</div>
	<div style="background-color: white;margin: 20px 0;padding: 20px 20px 0;">
		<div class="row">
			<div class="col-sm-12 text-right">
				<button onclick="func_freezeAll()" class="btn btn-white" type="button" style="border-color: #18a689;">批量冻结</button>
			</div>
		</div>
		<table id="myTable" class="display">
			<thead>
				<tr>
					<th style="width:44px;">
		            	<input id="checkedAll" type="checkbox">
					</th>
					<th>手机号码</th>
	                <th>消费金额</th>
	                <th>获得积分</th>
	                <th>剩余积分</th>
	                <th>积分状态</th>
	                <th>会员等级</th>
	                <th style="width: 160px;">操作</th>
				</tr>
			</thead>
		</table>
	</div>
</div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/bootcdn/BootCDN/ajax/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="<%=path%>/hplus/js/plugins/iCheck/icheck.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.15/js/jquery.dataTables.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/artDialog/7.0.0/dialog-plus.js"></script>
<script type="text/javascript">
var path = "<%=path%>";
var myDataTable;
var integralDetailDialog = null;

$(function(){
	init();
	
	$(document).on('ifChanged','#checkedAll', function(e){
		if(e.currentTarget.checked){
			$("#myTable>tbody :checkbox").iCheck('check');
		}else{
			$("#myTable>tbody :checkbox").iCheck('uncheck');
		}
	});
	
	$(document).on("click",".detail",function(){
		var memberId = $(this).attr("memberId");
		integralDetailDialog = dialog({
			id: "integralDetailDialog",
			fixed: true,
			padding: 0,
			width: 800,
			height: 240,
			title:'积分详情',
			url:path+"/view/member/level/integralDetail?memberId="+memberId,
			cancelDisplay: false,
			cancel: function () {
				this.remove();
			},
			okValue: "确定",
			ok: function () {
				this.close();
				this.remove();
			}
		});
		integralDetailDialog.showModal();
		$("iframe[name='integralDetailDialog']:visible").attr("scrolling","auto").css("overflow-x","hidden").css("overflow-y","auto");
	});
	
	$(document).on("click",".freeze",function(){
		var memberId = $(this).attr("memberId");
		var integralType = $(this).attr("integralType");
		$.post(path + "/scan/integral/updateIntegralType",{memberIds:memberId,integralType:integralType},function(data){
			if(data.msg_code == 1){
				func_dataTableReload(false);
			}
		},"json");
	});
	
	$(document).on("click",".changeIntegral",function(){
		var memberId = $(this).attr("memberId");
		var integralType = $(this).attr("integralType");
		
		if(integralType == 2){
			alert("已冻结，不允许修改");
			return ;
		}
		
		var d = dialog({
			fixed:true,
			title: '积分操作',
			content: '<table style="border-collapse:separate; border-spacing:0px 10px;"><tr><td style="width:80px;">加减积分</td><td><input id="integral" type="number" placeholder="正数为加积分；负数为减积分" style="width:240px;" class="form-control"></td></tr>'+
					'<tr><td>修改原因</td><td><input id="gain_reason" class="form-control"></td></tr></table>',
			okValue: "确定",
			ok: function () {
		 		$.post(path + "/scan/integral/changeIntegral",{memberId:memberId,integral:parseInt($("#integral").val()),gainReason:$("#gain_reason").val()},function(data){
		 			if(data.msg_code == 1){
		 				func_dataTableReload(false);
		 				alert("修改成功");
		 			}else{
		 				alert("修改失败");
		 			}
		 		},"json");
				
				this.close();
				this.remove();
			},
			cancelValue: "取消",
			cancel: true
		});
		d.show();
	});
});

function init(){
	myDataTable = dataTable();
	
	myDataTable.on( 'draw', function () {
		$(":checkbox").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",});
		$(":checkbox#checkedAll").iCheck('uncheck');
	});
}

//查询
function func_search(){
	func_dataTableReload();
}

//批量冻结
function func_freezeAll(){
	var memberIds = "";
	$("#myTable>tbody :checked").each(function(i,d){
		memberIds += "," + $(d).attr("memberId");
	});
	if(memberIds != ""){
		memberIds = memberIds.substr(1);
	}
	$.post(path + "/scan/integral/updateIntegralType",{memberIds:memberIds,integralType:2},function(data){
		if(data.msg_code == 1){
			func_dataTableReload(false);
		}
	},"json");
}

//查询条件重置
function func_reset(){
	$("#condition form")[0].reset();
	func_dataTableReload();
}

function func_dataTableReload(resetPaging) {
	if(resetPaging != undefined){
	    myDataTable.ajax.reload(null,resetPaging);
	}else{
		myDataTable.ajax.reload();
	}
}

function func_AjaxUrl() {
    return path + "/member/ajaxMemberPageList";
}

function func_columns() {
    var jsonArr = [
    	{"data": "memberId"},
        {"data": "phone"},
        {"data": "cost_total"},
        {"data": "gain_integral"},
        {"data": "crm_score"},
        {"data": "integral_type"},
        {"data": "crm_member_level"},
        {"data": "memberId"}
    ];
    return jsonArr;
}

function func_ajaxData() {
    var jsonObject = new Object();
    
    if($("#integral_type").val() != ""){
	    jsonObject.integral_type = $("#integral_type").val();	//积分状态
    }
    if($("#type").val() != ""){
	    jsonObject.type = $("#type").val();	//会员状态
    }
    if($.trim($("#phone").val()) != ""){
	    jsonObject.phone = $.trim($("#phone").val());
    }
    
    if($.trim($("#crm_member_level").val()) != ""){
	    jsonObject.crm_member_level = $.trim($("#crm_member_level").val());
    }
    
    if($.trim($("#cost_total1").val()) != ""){
	    jsonObject.cost_total1 = parseInt($.trim($("#cost_total1").val()))*100;
    }
    if($.trim($("#cost_total2").val()) != ""){
	    jsonObject.cost_total2 = parseInt($.trim($("#cost_total2").val()))*100;
    }
    
    if($.trim($("#point1").val()) != ""){
	    jsonObject.point1 = $.trim($("#point1").val());
    }
    if($.trim($("#point2").val()) != ""){
	    jsonObject.point2 = $.trim($("#point2").val());
    }
    
    if($.trim($("#use_integral1").val()) != ""){
	    jsonObject.use_integral1 = $.trim($("#use_integral1").val());
    }
    if($.trim($("#use_integral2").val()) != ""){
	    jsonObject.use_integral2 = $.trim($("#use_integral2").val());
    }
    
    return jsonObject;
}

function dataTable() {
    return $('#myTable').DataTable({
        processing: true,
        ordering: false,
        searching: false,
        info: true,
        autoWidth: true,
        lengthChange: false,
        language: {
            emptyTable: '没有数据表',
            info: '从第 _START_ 到第 _END_ 条数据；总共有 _TOTAL_ 条记录',
            infoEmpty: '没有数据',
            lengthMenu: '每页显示 _MENU_ 条记录',
            loadingRecords: '正在加载数据....',
            processing: '正在处理数据....',
            zeroRecords: '没有找到匹配的记录',
            paginate: {
                first: '首页',
                previous: '前页',
                next: '后页',
                last: '尾页'
            }
        },
        order: [[1, 'desc']],
        pageLength: 10,
        pagingType: 'full_numbers',
        columnDefs: [{
            render: function (data, type, full) {
                var result = "<input memberId='"+data+"' type='checkbox'>";
                return result;
            },
            targets: 0
        },{
            render: function (data, type, full) {
                var result = "";
                if(data != null){
                	result = data;
                }
                return result;
            },
            targets: 1
        },{
            render: function (data, type, full) {
                var result = "";
                result = formatMoney(data/100.0);
                return result;
            },
            targets: 2
        }, {
            render: function (data, type, full) {
                var result = "";
                if (data == 1) {
                    result = "正常";
                } else if(data == 2) {
                	result = "冻结";
                }
                return result;
            },
            targets: 5
        },{
            render: function (data, type, full) {
                var result = "";
                result += '<a memberId="'+data+'" class="detail">查看详情</a>&nbsp;&nbsp;&nbsp;&nbsp;';
                if(full.integral_type == 1){
	                result += '<a memberId="'+data+'" integralType="2" class="freeze">冻结</a>&nbsp;&nbsp;&nbsp;&nbsp;';
                }else if(full.integral_type == 2){
                	result += '<a memberId="'+data+'" integralType="1" class="freeze" style="color:red">解冻</a>&nbsp;&nbsp;&nbsp;&nbsp;';
                }
                result += '<a memberId="'+data+'" integralType="'+full.integral_type+'" class="changeIntegral">加减积分</a>';
                return result;
            },
            targets: 7
        }],
        columns: func_columns(),
        serverSide: true,
        ajax: {
            contentType: 'text/plain;charset=utf-8',
            type: 'POST',
            url: func_AjaxUrl(),
            data: function (d) {
                d.extra_search = func_ajaxData();
                return JSON.stringify(d);
            }
        }
    });
}

function formatMoney(number, places, symbol, thousand, decimal) {
    number = number || 0;
    places = !isNaN(places = Math.abs(places)) ? places : 2;
    symbol = symbol !== undefined ? symbol : "￥";
    thousand = thousand || ",";
    decimal = decimal || ".";
    var negative = number < 0 ? "-" : "",
        i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
        j = (j = i.length) > 3 ? j % 3 : 0;
    return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
}
</script>
</html>